package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.Pager;
import com.jason.system.common.util.RedisUtil;
import com.jason.system.entity.Number;
import com.jason.system.entity.Student;
import com.jason.system.service.StudentService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/25 15:18
 */
@RestController
@RequestMapping("/api/stu")
@Api(value = "/stu",tags = "学生信息接口")
public class StudentController {

  private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

  @Autowired
  StudentService studentService;

  @Autowired
  private RedisUtil redisUtil;

  @ApiOperation(value = "分页获取学生信息",notes = "页数，每页个数")
  @ApiImplicitParams(
          {@ApiImplicitParam(paramType = "query",name = "page", value = "页数",  dataType = "Integer"),
                  @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",dataType = "Integer")}
  )
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取所有学生信息成功"),
          @ApiResponse(code = 201,message = "获取所有学生信息失败")
  })
  @GetMapping("/getAll")
  public JSONObject getAllStudent(@RequestParam("size") Integer size ,@RequestParam("page") Integer page){
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    Pager<Student> studentPager = studentService.getAllStudent(page,size);
    logger.info("===>>获取的students对象==>>>: {}",studentPager);
    if (studentPager!=null){
      logger.info("==>>>StudentController==>>>获取所有学生信息成功.....");
      r.setCode(200);
      r.setMessage("获取所有学生信息成功");
      r.setData(studentPager);
    }else {
      logger.info("==>>>StudentController==>>>获取所有学生信息失败.....");
      r.setCode(201);
      r.setMessage("获取所有学生信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @GetMapping("/getAllStudent")
  public JSONObject getAllStudent(){
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/getAllStudents.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    List<Student> studentList = studentService.getAllStudent();
    logger.info("===>>获取的students对象==>>>: {}",studentList);
    if (studentList!=null){
      logger.info("==>>>StudentController==>>>获取所有学生信息成功.....");
      r.setCode(200);
      r.setMessage("获取所有学生信息成功");
      r.setData(studentList);
    }else {
      logger.info("==>>>StudentController==>>>获取所有学生信息失败.....");
      r.setCode(201);
      r.setMessage("获取所有学生信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "添加学生", notes = "添加学生信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "body",name = "stuNo",value = "学号", required = true, dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "stuName",value = "学生姓名",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "grade",value = "年级",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "major",value = "专业",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "number",value = "电话",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "添加学生信息成功"),
          @ApiResponse(code = 201,message = "学生信息已存在"),
          @ApiResponse(code = 202,message = "添加学生信息失败"),
  })
  @PostMapping("/insert")
  public JSONObject insertStudent( Student insertStudent){
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/insert.....");
    JSONObject jsonObject = new JSONObject();
    Student student = studentService.getStudentByNo(insertStudent.getStuNo());
    logger.info("===>>StudentController==>>>获取的student对象:{}",student);
    ResultEntity r = new ResultEntity();
    if(student!=null){
      r.setCode(201);
      r.setMessage("学生信息已存在");
      logger.info("===>>StudentController==>>>学生信息已存在:{}",student);
    }else {
      try {
        r.setCode(200);
        r.setMessage("添加学生信息成功");
        studentService.insertStudent(insertStudent);
        logger.info("===>>StudentController==>>>学生信息添加成功.....");
      }catch (Exception e ){
        logger.info("===>>StudentController==>>>学生信息添加失败.....");
        r.setCode(202);
        r.setMessage("添加学生信息失败");
      }
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "修改学生信息", notes = "修改学生信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "body",name = "stuNo",value = "学号", required = true, dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "stuName",value = "学生姓名",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "grade",value = "年级",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "major",value = "专业",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "body",name = "number",value = "电话",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "修改学生信息成功"),
          @ApiResponse(code = 201,message = "修改学生信息失败")
  })
  @PostMapping("/update")
  public JSONObject updateStudent( Student student){
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/update.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>StudentController==>>>正在更新student中.....");
    try {
      studentService.updateStudent(student);
      logger.info("==>>>StudentController==>>>更新student信息完成.....");
      r.setCode(200);
      r.setMessage("修改学生信息成功");
    }catch (Exception e){
      logger.info("==>>>StudentController==>>>更新student信息失败.....");
      r.setCode(201);
      r.setMessage("修改学生信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }


  @ApiOperation(value = "删除学生", notes = "删除学生信息")
  @ApiImplicitParam(paramType = "path",name = "stuId",value = "学生id", required = true, dataType = "Integer")
  @ApiResponses({
          @ApiResponse(code = 200,message = "删除学生信息成功"),
          @ApiResponse(code = 201,message = "删除学生信息失败")
  })
  @PostMapping("/delete/{sId}")
  public JSONObject deleteStudent(@PathVariable("sId") Integer sId){
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/delete.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>StudentController==>>>正在删除student信息中.....");
    boolean result = studentService.deleteStudent(sId);
    if (result){
      r.setCode(200);
      r.setMessage("删除学生信息成功");
      logger.info("==>>>StudentController==>>>删除student信息完成.....");
    }else {
      r.setCode(201);
      r.setMessage("删除学生信息失败");
      logger.info("==>>>StudentController==>>>删除student信息失败.....");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "批量删除学生", notes = "批量删除学生信息")
  @ApiImplicitParam(paramType = "query",name = "stuIds",value = "学生id集合", required = true, dataType = "String")
  @ApiResponses({
          @ApiResponse(code = 200,message = "批量删除学生信息成功"),
          @ApiResponse(code = 201,message = "批量删除学生信息失败")
  })
  @PostMapping("/deleteStudents")
  public JSONObject deleteMoreStudent(String sIds){
    //使用流将string字符串转换成List集合
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/deleteStudents.....");
    List<Integer> studentIds = Arrays.stream(sIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>StudentController==>>>正在批量删除student中.....");
    boolean result = studentService.deleteMoreStudent(studentIds);
    if (result){
      r.setCode(200);
      r.setMessage("批量删除学生信息成功");
      logger.info("==>>>StudentController==>>>批量删除student信息完成.....");
    }else {
      r.setCode(201);
      r.setMessage("批量删除学生信息失败");
      logger.info("==>>>StudentController==>>>批量删除student信息失败.....");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @PostMapping("/search")
  public JSONObject searchStudent(Integer page , Integer size , String grade , String major){
    logger.info("==>>>StudentController==>>>进行路径：/api/stu/searchStudents.....");
    logger.info("==>>>StudentController==>>>grade:{}",grade);
    logger.info("==>>>StudentController==>>>major:{}",major);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>StudentController==>>>正在查询students中.....");
    Pager<Student> studentPager = studentService.searchStudents(page,size, grade,major);
    logger.info("==>>>StudentController==>>>numbers:{}",studentPager);
    if (studentPager!=null){
      logger.info("==>>>StudentController==>>>查询students完成.....");
      r.setCode(200);
      r.setMessage("查询学生信息成功");
      r.setData(studentPager);
    }else {
      logger.info("==>>>StudentController==>>>查询students失败.....");
      r.setCode(201);
      r.setMessage("查询学生信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

}
